<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\FileUpload;
use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\Articles;
use app\api\model\v1\ArticleType;
use app\BaseController;
use think\facade\Env;
use think\Request;

class Article extends BaseController
{
    use SearchDataForModel,FileUpload;

    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 10:09
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源列表
     */
    public function getArticleList(){
        $limit = $this->request->param('limit',10);
        $map = [];
        if ($this->request->has('is_hot') && !empty($this->request->param('is_hot'))) {
            $map[] = ['is_hot', '=', $this->request->param('is_hot')];
        }
        $map[] = ['status', '=', 1];
        $res = Articles::where($map)->with(['articleType'])->paginate($limit)->each(function ($item, $key) {
            $item['url_path'] = Env::get('HTTP.http', 'HTTP.default_http') . 'storage/' . $item['path'];
            return $item;
        });
        return $this->response($res);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 10:04
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存文章
     */
    public function saveArticle()
    {
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['create_id'] = $this->request->uid;
        if (intval($id) === 0) {
            Articles::create($data);
        } else {
            $article = Articles::find(intval($id));
            $article->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '发布文章' : '更新文章', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * @param $id
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 10:58
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除文章
     */
    public function delArticle($id){
        $ids = explode(',', $id);
        foreach ($ids as $id) {
            $article = Articles::find($id);
            if (is_null($article)) {
                return $this->response(404, '文章不存在');
            }
            //->force()
            $article->delete();
            AdminLog::record('删除文章', $this->request->param(), $this->request->uid, $this->request->username);
        }
        return $this->response();
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 9:35
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 文章类别列表
     */
    public function getArticleTypeList()
    {
        $type = $this->request->param('type','no_all');

        $res = ArticleType::select()->toArray();
        if ($type === 'all'){
            array_unshift($res,[
                'id'=>0,
                'name'=>'全部'
            ]);
        }
        return $this->response($res);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 17:59
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 微信端获取文章
     */
    public function getWxArticleList(){
        $limit = $this->request->param('limit',10);
        $article_type_id = $this->request->param('article_type_id',0);

        $map = [];
        if (intval($article_type_id) !== 0){
            $map[] = array('article_type_id','=',$article_type_id);
        }
        $map[] = array('status','=',1);
        $res = Articles::where($map)->order('create_time desc')->paginate($limit)->each(function ($item, $key) {
            $item['url_path'] = Env::get('HTTP.http', 'HTTP.default_http') . 'storage/' . $item['path'];
            return $item;
        });
        return $this->response($res);
    }


    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 9:46
     * @return \think\response\Json
     * 上传图片
     */
    public function UploadImg()
    {
        if (!$this->request->file('file')) {
            return $this->response(501, '请选择上传文件');
        }
        $file = $this->request->file('file');
        $result = $this->save($file, 'article_img');
        return $this->response($result);
    }

    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 9:47
     * @return \think\response\Json
     * 删除文件
     */
    public function delFile()
    {
        $data = $this->request->param();
        $type = $this->request->param('type', 1);
        if (strstr($data['file'], 'https') !== false) {
            $file_a = explode('storage', $data['file']);
            $file_s = (substr($file_a[1], 1));
        } else {
            $file_s = $data['file'];
        }
        if ($type === 2) {
            $uid = $this->request->param('uid');
            $name = $this->request->param('name');
            $type = substr(strrchr($name, "."), 1);
            $file = app()->getRootPath() . 'public/storage/record/' . $uid . '.' . $type;
        } else {
            $file = app()->getRootPath() . 'public/storage/' . $file_s;
        }
        if (!empty($data['file'])) {
            if (file_exists($file)) {
                unlink($file);
            }
        }
        return $this->response();
    }


    /**
     * @User: 刘海龙
     * @Date: 2021/10/8
     * @Time: 17:39
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取文章信息
     */
    public function getShowArticle(){
        $id = $this->request->param('id',0);
        $res = Articles::find($id);
        if (is_null($res)){
            return $this->response(501, '暂未找到该文章');
        }
        $res['url_path'] = Env::get('HTTP.http', 'HTTP.default_http') . 'storage/' . $res['path'];
        return $this->response($res->toArray());
    }
}
